package com.zidian.leader.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.zidian.leader.R;
import com.zidian.leader.adapter.TeachingQualityAdapter;
import com.zidian.leader.api.HttpFactory;
import com.zidian.leader.common.activity.BaseActivity;
import com.zidian.leader.common.util.HawkUtils;
import com.zidian.leader.common.util.StringConstants;
import com.zidian.leader.entity.LoginResult;
import com.zidian.leader.entity.StudentEvaSummary;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by GongCheng on 2016/10/24 0024.
 * 教学质量
 */

public class TeachingQualityActivity extends BaseActivity {

    @Bind(R.id.select_ll)
    LinearLayout selectLl;
    @Bind(R.id.school_select_tv)
    TextView schoolSelectTv;
    @Bind(R.id.college_select_sp)
    Spinner collegeSelectSp;
    @Bind(R.id.teacher_eva_title_tv)
    TextView teacherEvaTitleTv;
    @Bind(R.id.eva_others_tv)
    TextView evaOthersTv;
    @Bind(R.id.student_eva_rv)
    RecyclerView studentEvaRv;

    private List<String> colleges;
    private String college;
    private TeachingQualityAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_teacher_evaluation;
    }

    private void initView() {
        //Toolbar
        actionBar.setTitle(R.string.teaching_quality);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_24dp);

        //点击防抖
        Subscription viewSubscription1 = RxView.clicks(evaOthersTv)
                .throttleFirst(1000, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        onEvaDetailClick();
                    }
                });
        Subscription viewSubscription2 = RxView.clicks(schoolSelectTv)
                .throttleFirst(1500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        onSchoolSelect();
                    }
                });
        addSubscription(viewSubscription1);
        addSubscription(viewSubscription2);
        adapter = new TeachingQualityAdapter();
        studentEvaRv.setLayoutManager(new LinearLayoutManager(this));
        studentEvaRv.setAdapter(adapter);

        adapter.setOnTwoIndexSelectListener(new TeachingQualityAdapter.OnTwoIndexSelectListener() {
            @Override
            public void onTwoIndexSelected(String twoIndexName) {
                Intent intent = new Intent(TeachingQualityActivity.this, StudentEvaTwoIndexActivity.class);
                intent.putExtra("twoIndexName", twoIndexName);
                intent.putExtra("collegeName", college);
                startActivity(intent);
            }
        });
        //获取数据(院长校长显示界面不一样)
        if (HawkUtils.getRoleId() == 6) {
            selectLl.setVisibility(View.GONE);
            college = HawkUtils.getColleges().get(0).getCollege().getCollegeName();
            getStudentEvaSummary(college);
        } else {
            initSpinner();
            college = "";
            getStudentEvaSummary(null);
        }
    }

    /**
     * 初始化Spinner
     */
    private void initSpinner() {
        colleges = new ArrayList<>();
        college = null;
        List<LoginResult.DataBean.CollegeListBean> data = HawkUtils.getColleges();
        if (data == null) {
            Snackbar.make(evaOthersTv, "加载数据失败", Snackbar.LENGTH_SHORT).show();
            return;
        }
        colleges.add("学院");
        //存储学院名
        for (int i = 0; i < data.size(); i++) {
            colleges.add(data.get(i).getCollege().getCollegeName());
        }

        ArrayAdapter arrayAdapter = new ArrayAdapter<>(TeachingQualityActivity.this,
                R.layout.spinner_item, colleges);
        arrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);

        collegeSelectSp.setAdapter(arrayAdapter);
        collegeSelectSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    return;
                }
                college = colleges.get(position);
                getStudentEvaSummary(college);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    /**
     * 查看其他评价
     */
    @OnClick(R.id.eva_others_tv)
    public void onEvaDetailClick() {
        Intent intent = new Intent(this, ColleagueEvaluationActivity.class);
        intent.putExtra("collegeName", college);
        startActivity(intent);
    }

    /**
     * 查看全校
     */
    @OnClick(R.id.school_select_tv)
    public void onSchoolSelect() {
        college = "";
        collegeSelectSp.setSelection(0);
        getStudentEvaSummary(null);
    }

    /**
     * 得到学生评价概要
     * @param collegeName
     */
    private void getStudentEvaSummary(String collegeName) {
        subscription = HttpFactory.getInstance().getStudentEvaSummary(collegeName,
                HawkUtils.getUsername(), HawkUtils.getRoleId() + StringConstants.STRING_EMPTY, HawkUtils.getToken())
                .subscribe(new Subscriber<List<StudentEvaSummary>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        progressDialog.show();
                    }

                    @Override
                    public void onCompleted() {
                        progressDialog.dismiss();
                    }

                    @Override
                    public void onError(Throwable e) {
                        progressDialog.dismiss();
                        adapter.setList(null);
                        Snackbar.make(evaOthersTv, e.getMessage(), Snackbar.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(List<StudentEvaSummary> studentEvaSummaries) {
                        adapter.setList(studentEvaSummaries);
                    }
                });
    }

}
